/*
 * Copyright 2012 Faculty of Informatics - Masaryk University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.pa165.creatures.model;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 * This class represents a weapon we use against the {@link CreatureType}.
 *
 * @author xmiklos1
 */
@Entity
@Table(uniqueConstraints = {
    @UniqueConstraint(columnNames = "NAME")
})
@XmlRootElement
public class Weapon implements Serializable {

    private static final long serialVersionUID = -2668052887083648198L;

    /**
     * Id of the weapon.
     */
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "ID", nullable = false)
    private Long id;

    /**
     * Name of the weapon.
     */
    @Column(name = "NAME", nullable = false)
    private String name;

    /**
     * Range of the weapon in meters (if applicable).
     */
    @Column(name = "RANGE")
    private Integer range;

    /**
     * Number of bullets in one full loaded weapon (if applicable).
     */
    @Column(name = "AMMUNITION")
    private Integer ammunition;

    @OneToMany(mappedBy = "weapon")
    @XmlTransient
    private List<CreatureTypeWeapon> creatureTypes;

    public Weapon() {
    }

    /**
     *
     * @param name name of the weapon
     */
    public Weapon(String name) {
        this.name = name;
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getRange() {
        return this.range;
    }

    public void setRange(Integer range) {
        this.range = range;
    }

    public Integer getAmmunition() {
        return this.ammunition;
    }

    public void setAmmunition(Integer ammunition) {
        this.ammunition = ammunition;
    }

    public List<CreatureTypeWeapon> getCreatureTypes() {
        return creatureTypes;
    }

    public void setCreatureTypes(List<CreatureTypeWeapon> creatureTypes) {
        this.creatureTypes = creatureTypes;
    }

    @Override
    public String toString() {
        return "Weapon{" + "id=" + id + ", name=" + name + ", range=" + range
                + ", ammunition=" + ammunition + '}';
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 73 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Weapon other = (Weapon) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

}
